package com.xyz.service.impl;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xyz.entity.mapper.*;
import com.xyz.entity.pojo.*;
import com.xyz.service.BaseService;
import com.xyz.util.dto.DataResult;
import com.xyz.util.dto.GridData;
import com.xyz.util.dto.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class BaseServiceImpl implements BaseService {

    @Autowired
    private SystemDictTypeMapper systemDictTypeMapper;

    @Autowired
    private SystemDictDataMapper systemDictDataMapper;

    @Autowired
    private EquipmentInfoMapper equipmentInfoMapper;

    @Autowired
    private EquipmentTreeMapper equipmentTreeMapper;

    @Autowired
    private PatrolTemplateTaskMapper patrolTemplateTaskMapper;

    @Autowired
    private PatrolTemplateSubsystemMapper patrolTemplateSubsystemMapper;

    @Autowired
    private PatrolTemplateFixedMapper patrolTemplateFixedMapper;

    @Override
    public DataResult selectDictType(LoginInfo li, int pageNum, int pageSize, String name, String code) throws Exception {
        SystemDictTypeExample sdtExample = new SystemDictTypeExample();
        SystemDictTypeExample.Criteria sdtCriteria = sdtExample.createCriteria();
        if (StrUtil.isNotBlank(name)) {
            sdtCriteria.andDictTypeNameLike("%" + name + "%");
        }
        if (StrUtil.isNotBlank(code)) {
            sdtCriteria.andDictTypeCodeLike("%" + code + "%");
        }
        sdtCriteria.andIsDeleteEqualTo(0);
        sdtExample.setOrderByClause("dict_type_sort, create_time desc");
        Page<SystemDictType> page = PageHelper.startPage(pageNum, pageSize);
        List<SystemDictType> sdtList = systemDictTypeMapper.selectByExample(sdtExample);
        List<Map<String, Object>> list = new ArrayList<>();
        for (SystemDictType sdt : sdtList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", sdt.getId());
            map.put("name", sdt.getDictTypeName());
            map.put("code", sdt.getDictTypeCode());
            map.put("sort", sdt.getDictTypeSort());
            map.put("remark", sdt.getRemark());
            list.add(map);
        }
        return DataResult.build9100(new GridData(page.getTotal(), list));
    }

    @Override
    public DataResult insertDictType(LoginInfo li, String name, String code, String sort, String remark) throws Exception {
        SystemDictTypeExample sdtExample = new SystemDictTypeExample();
        SystemDictTypeExample.Criteria sdtCriteria1 = sdtExample.createCriteria();
        SystemDictTypeExample.Criteria sdtCriteria2 = sdtExample.or();
        sdtCriteria1.andDictTypeNameEqualTo(name);
        sdtCriteria1.andIsDeleteEqualTo(0);
        sdtCriteria2.andDictTypeCodeEqualTo(code);
        sdtCriteria2.andIsDeleteEqualTo(0);
        long count = systemDictTypeMapper.countByExample(sdtExample);
        if (count > 0) {
            return DataResult.build9250("新增失败，名称或编码重复");
        }

        SystemDictType newSdt = new SystemDictType();
        newSdt.setDictTypeName(name);
        newSdt.setDictTypeCode(code);
        newSdt.setDictTypeSort(NumberUtil.isNumber(sort) ? Integer.parseInt(sort) : 0);
        newSdt.setRemark(StrUtil.isBlank(remark) ? "" : remark);
        newSdt.setIsDelete(0);
        newSdt.setCreateTime(new Date());
        systemDictTypeMapper.insert(newSdt);
        return DataResult.build9100(newSdt.getId());
    }

    @Override
    public DataResult updateDictType(LoginInfo li, long id, String name, String code, String sort, String remark) throws Exception {
        SystemDictTypeExample sdtExample = new SystemDictTypeExample();
        SystemDictTypeExample.Criteria sdtCriteria1 = sdtExample.createCriteria();
        SystemDictTypeExample.Criteria sdtCriteria2 = sdtExample.or();
        sdtCriteria1.andIdNotEqualTo(id);
        sdtCriteria1.andDictTypeNameEqualTo(name);
        sdtCriteria1.andIsDeleteEqualTo(0);
        sdtCriteria2.andIdNotEqualTo(id);
        sdtCriteria2.andDictTypeCodeEqualTo(code);
        sdtCriteria2.andIsDeleteEqualTo(0);
        long count = systemDictTypeMapper.countByExample(sdtExample);
        if (count > 0) {
            return DataResult.build9250("修改失败，名称或编码重复");
        }

        SystemDictType newSdt = new SystemDictType();
        newSdt.setId(id);
        newSdt.setDictTypeName(name);
        newSdt.setDictTypeCode(code);
        newSdt.setDictTypeSort(NumberUtil.isNumber(sort) ? Integer.parseInt(sort) : 0);
        newSdt.setRemark(StrUtil.isBlank(remark) ? "" : remark);
        systemDictTypeMapper.updateByPrimaryKeySelective(newSdt);
        return DataResult.build9100(id);
    }

    @Override
    public DataResult deleteDictType(LoginInfo li, long id) throws Exception {
        SystemDictType newSdt = new SystemDictType();
        newSdt.setId(id);
        newSdt.setIsDelete(1);
        systemDictTypeMapper.updateByPrimaryKeySelective(newSdt);

        SystemDictData newSdd = new SystemDictData();
        newSdd.setIsDelete(1);
        SystemDictDataExample sddExample = new SystemDictDataExample();
        SystemDictDataExample.Criteria sddCriteria = sddExample.createCriteria();
        sddCriteria.andDictTypeIdEqualTo(id);
        systemDictDataMapper.updateByExample(newSdd, sddExample);

        return DataResult.build9100(id);
    }

    @Override
    public DataResult selectDictData(LoginInfo li, int pageNum, int pageSize, long id) throws Exception {
        SystemDictDataExample sddExample = new SystemDictDataExample();
        SystemDictDataExample.Criteria sddCriteria = sddExample.createCriteria();
        sddCriteria.andDictTypeIdEqualTo(id);
        sddCriteria.andIsDeleteEqualTo(0);
        sddExample.setOrderByClause("dict_sort, create_time desc");
        Page<SystemDictData> page = PageHelper.startPage(pageNum, pageSize);
        List<SystemDictData> sddList = systemDictDataMapper.selectByExample(sddExample);
        List<Map<String, Object>> list = new ArrayList<>();
        for (SystemDictData sdd : sddList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", sdd.getId());
            map.put("dictTypeId", sdd.getDictTypeId());
            map.put("name", sdd.getDictName());
            map.put("value", sdd.getDictValue());
            map.put("sort", sdd.getDictSort());
            map.put("color", sdd.getColor());
            map.put("remark", sdd.getRemark());
            list.add(map);
        }
        return DataResult.build9100(new GridData(page.getTotal(), list));
    }

    @Override
    public DataResult insertDictData(LoginInfo li, long id, String name, int value, String sort, String color, String remark) throws Exception {
        SystemDictDataExample sddExample = new SystemDictDataExample();
        SystemDictDataExample.Criteria sdtCriteria = sddExample.createCriteria();
        sdtCriteria.andDictTypeIdEqualTo(id);
        sdtCriteria.andDictValueEqualTo(value);
        sdtCriteria.andIsDeleteEqualTo(0);
        long count = systemDictDataMapper.countByExample(sddExample);
        if (count > 0) {
            return DataResult.build9250("新增失败，值重复");
        }

        SystemDictData newSdd = new SystemDictData();
        newSdd.setDictTypeId(id);
        newSdd.setDictName(name);
        newSdd.setDictValue(value);
        newSdd.setDictSort(NumberUtil.isNumber(sort) ? Integer.parseInt(sort) : 0);
        newSdd.setRemark(StrUtil.isBlank(remark) ? "" : remark);
        newSdd.setColor(StrUtil.isBlank(color) ? "" : color);
        newSdd.setIsDelete(0);
        newSdd.setCreateTime(new Date());
        systemDictDataMapper.insert(newSdd);
        return DataResult.build9100(newSdd.getId());
    }

    @Override
    public DataResult updateDictData(LoginInfo li, long id, String name, int value, String sort, String color, String remark) throws Exception {
        SystemDictData sdd = systemDictDataMapper.selectByPrimaryKey(id);

        SystemDictDataExample sddExample = new SystemDictDataExample();
        SystemDictDataExample.Criteria sdtCriteria = sddExample.createCriteria();
        sdtCriteria.andIdNotEqualTo(id);
        sdtCriteria.andDictTypeIdEqualTo(sdd.getDictTypeId());
        sdtCriteria.andDictValueEqualTo(value);
        sdtCriteria.andIsDeleteEqualTo(0);
        long count = systemDictDataMapper.countByExample(sddExample);
        if (count > 0) {
            return DataResult.build9250("新增失败，值重复");
        }

        SystemDictData newSdd = new SystemDictData();
        newSdd.setId(id);
        newSdd.setDictName(name);
        newSdd.setDictValue(value);
        newSdd.setDictSort(NumberUtil.isNumber(sort) ? Integer.parseInt(sort) : 0);
        newSdd.setColor(StrUtil.isBlank(color) ? "" : color);
        newSdd.setRemark(StrUtil.isBlank(remark) ? "" : remark);
        systemDictDataMapper.updateByPrimaryKeySelective(newSdd);
        return DataResult.build9100(id);
    }

    @Override
    public DataResult deleteDictData(LoginInfo li, long id) throws Exception {
        SystemDictData newSdd = new SystemDictData();
        newSdd.setId(id);
        newSdd.setIsDelete(1);
        systemDictDataMapper.updateByPrimaryKeySelective(newSdd);
        return DataResult.build9100(id);
    }

    @Override
    public DataResult insertArea(LoginInfo li, String name, String code, String sort) throws Exception {
        EquipmentTreeExample etExample = new EquipmentTreeExample();
        EquipmentTreeExample.Criteria etCriteria = etExample.createCriteria();
        etCriteria.andCodeEqualTo(code);
        etCriteria.andIsDeleteEqualTo(0);
        int count = (int) equipmentTreeMapper.countByExample(etExample);
        if (count > 0) {
            return DataResult.build9250("新增失败，编码重复");
        }
        EquipmentTree newEt = new EquipmentTree();
        newEt.setType(0);
        newEt.setBaseId(0L);
        newEt.setName(name);
        newEt.setSort(StrUtil.isBlank(sort) ? 0 : Integer.parseInt(sort));
        newEt.setEmType(0);
        newEt.setCode(code);
        newEt.setIsDelete(0);
        equipmentTreeMapper.insertSelective(newEt);
        return DataResult.build9100(newEt.getId());
    }

    @Override
    public DataResult updateArea(LoginInfo li, long id, String name, String code, String sort) throws Exception {
        EquipmentTreeExample etExample = new EquipmentTreeExample();
        EquipmentTreeExample.Criteria etCriteria = etExample.createCriteria();
        etCriteria.andIdNotEqualTo(id);
        etCriteria.andCodeEqualTo(code);
        etCriteria.andIsDeleteEqualTo(0);
        int count = (int) equipmentTreeMapper.countByExample(etExample);
        if (count > 0) {
            return DataResult.build9250("修改失败，编码重复");
        }
        EquipmentTree newEt = new EquipmentTree();
        newEt.setId(id);
        newEt.setName(name);
        newEt.setSort(StrUtil.isBlank(sort) ? 0 : Integer.parseInt(sort));
        newEt.setCode(code);
        equipmentTreeMapper.updateByPrimaryKeySelective(newEt);
        return DataResult.build9100(newEt.getId());
    }

    @Override
    public DataResult deleteArea(LoginInfo li, long id) throws Exception {
        EquipmentTreeExample etExample = new EquipmentTreeExample();
        EquipmentTreeExample.Criteria etCriteria = etExample.createCriteria();
        etCriteria.andTypeEqualTo(1);
        etCriteria.andBaseIdEqualTo(id);
        etCriteria.andIsDeleteEqualTo(0);
        int count = (int) equipmentTreeMapper.countByExample(etExample);
        if (count > 0) {
            return DataResult.build9250("删除失败，该区域下存在系统");
        }

        EquipmentTree newEt = new EquipmentTree();
        newEt.setId(id);
        newEt.setIsDelete(1);
        equipmentTreeMapper.updateByPrimaryKeySelective(newEt);
        return DataResult.build9100(id);
    }

    @Override
    public DataResult insertSystem(LoginInfo li, long areaTreeId, String name, String code, String sort) throws Exception {
        EquipmentTreeExample etExample = new EquipmentTreeExample();
        EquipmentTreeExample.Criteria etCriteria = etExample.createCriteria();
        etCriteria.andCodeEqualTo(code);
        etCriteria.andIsDeleteEqualTo(0);
        int count = (int) equipmentTreeMapper.countByExample(etExample);
        if (count > 0) {
            return DataResult.build9250("新增失败，编码重复");
        }
        EquipmentTree newEt = new EquipmentTree();
        newEt.setType(1);
        newEt.setBaseId(areaTreeId);
        newEt.setName(name);
        newEt.setSort(StrUtil.isBlank(sort) ? 0 : Integer.parseInt(sort));
        newEt.setEmType(0);
        newEt.setCode(code);
        newEt.setIsDelete(0);
        equipmentTreeMapper.insertSelective(newEt);
        return DataResult.build9100(newEt.getId());
    }

    @Override
    public DataResult updateSystem(LoginInfo li, long id, long areaTreeId, String name, String code, String sort) throws Exception {
        EquipmentTreeExample etExample = new EquipmentTreeExample();
        EquipmentTreeExample.Criteria etCriteria = etExample.createCriteria();
        etCriteria.andIdNotEqualTo(id);
        etCriteria.andCodeEqualTo(code);
        etCriteria.andIsDeleteEqualTo(0);
        int count = (int) equipmentTreeMapper.countByExample(etExample);
        if (count > 0) {
            return DataResult.build9250("修改失败，编码重复");
        }
        EquipmentTree newEt = new EquipmentTree();
        newEt.setId(id);
        newEt.setBaseId(areaTreeId);
        newEt.setName(name);
        newEt.setSort(StrUtil.isBlank(sort) ? 0 : Integer.parseInt(sort));
        newEt.setCode(code);
        equipmentTreeMapper.updateByPrimaryKeySelective(newEt);
        return DataResult.build9100(newEt.getId());
    }

    @Override
    public DataResult deleteSystem(LoginInfo li, long id) throws Exception {
        EquipmentTreeExample etExample = new EquipmentTreeExample();
        EquipmentTreeExample.Criteria etCriteria = etExample.createCriteria();
        etCriteria.andTypeEqualTo(2);
        etCriteria.andBaseIdEqualTo(id);
        etCriteria.andIsDeleteEqualTo(0);
        int count = (int) equipmentTreeMapper.countByExample(etExample);
        if (count > 0) {
            return DataResult.build9250("删除失败，该系统下存在子系统");
        }

        EquipmentTree newEt = new EquipmentTree();
        newEt.setId(id);
        newEt.setIsDelete(1);
        equipmentTreeMapper.updateByPrimaryKeySelective(newEt);
        return DataResult.build9100(id);
    }

    @Override
    public DataResult insertSubsystem(LoginInfo li, long subsystemTreeId, String name, int emType, String code, String sort) throws Exception {
        EquipmentTreeExample etExample = new EquipmentTreeExample();
        EquipmentTreeExample.Criteria etCriteria = etExample.createCriteria();
        etCriteria.andCodeEqualTo(code);
        etCriteria.andIsDeleteEqualTo(0);
        int count = (int) equipmentTreeMapper.countByExample(etExample);
        if (count > 0) {
            return DataResult.build9250("新增失败，编码重复");
        }
        EquipmentTree newEt = new EquipmentTree();
        newEt.setType(2);
        newEt.setBaseId(subsystemTreeId);
        newEt.setName(name);
        newEt.setSort(StrUtil.isBlank(sort) ? 0 : Integer.parseInt(sort));
        newEt.setEmType(emType);
        newEt.setCode(code);
        newEt.setIsDelete(0);
        equipmentTreeMapper.insertSelective(newEt);
        return DataResult.build9100(newEt.getId());
    }

    @Override
    public DataResult updateSubsystem(LoginInfo li, long id, long subsystemTreeId, String name, int emType, String code, String sort) throws Exception {
        EquipmentTreeExample etExample = new EquipmentTreeExample();
        EquipmentTreeExample.Criteria etCriteria = etExample.createCriteria();
        etCriteria.andIdNotEqualTo(id);
        etCriteria.andCodeEqualTo(code);
        etCriteria.andIsDeleteEqualTo(0);
        int count = (int) equipmentTreeMapper.countByExample(etExample);
        if (count > 0) {
            return DataResult.build9250("修改失败，编码重复");
        }
        EquipmentTree newEt = new EquipmentTree();
        newEt.setId(id);
        newEt.setBaseId(subsystemTreeId);
        newEt.setName(name);
        newEt.setSort(StrUtil.isBlank(sort) ? 0 : Integer.parseInt(sort));
        newEt.setEmType(emType);
        newEt.setCode(code);
        equipmentTreeMapper.updateByPrimaryKeySelective(newEt);
        return DataResult.build9100(newEt.getId());
    }

    @Override
    public DataResult deleteSubsystem(LoginInfo li, long id) throws Exception {
        EquipmentInfoExample eiExample = new EquipmentInfoExample();
        EquipmentInfoExample.Criteria eiCriteria = eiExample.createCriteria();
        eiCriteria.andEmTreeEqualTo(id);
        eiCriteria.andIsDeleteEqualTo(0);
        int count1 = (int) equipmentInfoMapper.countByExample(eiExample);
        if (count1 > 0) {
            return DataResult.build9250("删除失败，该子系统下存在设备");
        }
        EquipmentTreeExample etExample = new EquipmentTreeExample();
        EquipmentTreeExample.Criteria etCriteria = etExample.createCriteria();
        etCriteria.andTypeEqualTo(3);
        etCriteria.andBaseIdEqualTo(id);
        etCriteria.andIsDeleteEqualTo(0);
        int count2 = (int) equipmentTreeMapper.countByExample(etExample);
        if (count2 > 0) {
            return DataResult.build9250("删除失败，该子系统下存在设备组合");
        }

        EquipmentTree newEt = new EquipmentTree();
        newEt.setId(id);
        newEt.setIsDelete(1);
        equipmentTreeMapper.updateByPrimaryKeySelective(newEt);
        return DataResult.build9100(id);
    }

    @Override
    public DataResult selectPatrolEm(LoginInfo li, int pageNum, int pageSize, String name) throws Exception {
        List<Map<String, Object>> list = new ArrayList<>();

        SystemDictTypeExample sdtExample = new SystemDictTypeExample();
        sdtExample.createCriteria().andDictTypeCodeEqualTo("TYPE_EM_TYPE");
        List<SystemDictType> sdtList = systemDictTypeMapper.selectByExample(sdtExample);
        if (sdtList.size() != 1) {
            return DataResult.build9100(new GridData(0, list));
        }

        SystemDictDataExample sddExample = new SystemDictDataExample();
        SystemDictDataExample.Criteria sddCriteria = sddExample.createCriteria();
        if (StrUtil.isNotBlank(name)) {
            sddCriteria.andDictNameLike("%" + name + "%");
        }
        sddCriteria.andDictTypeIdEqualTo(sdtList.get(0).getId()).andIsDeleteEqualTo(0);
        sddExample.setOrderByClause("dict_sort asc, create_time desc");
        Page<SystemDictData> page = PageHelper.startPage(pageNum, pageSize);
        List<SystemDictData> sddList = systemDictDataMapper.selectByExample(sddExample);
        for (SystemDictData sdd : sddList) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", sdd.getDictName());
            map.put("emType", sdd.getDictValue());
            map.put("sort", sdd.getDictSort());
            map.put("remark", sdd.getRemark());
            list.add(map);
        }
        return DataResult.build9100(new GridData(page.getTotal(), list));
    }

    @Override
    public DataResult selectPatrolSubsystem(LoginInfo li, String pageNum, String pageSize, String subsystemTreeId, String name) throws Exception {
        EquipmentTreeExample etExample = new EquipmentTreeExample();
        etExample.createCriteria().andTypeEqualTo(2).andIsDeleteEqualTo(0);
        ;
        List<EquipmentTree> etList = equipmentTreeMapper.selectByExample(etExample);
        Map<Long, EquipmentTree> etMap = CollStreamUtil.toIdentityMap(etList, EquipmentTree::getId);

        PatrolTemplateSubsystemExample ptsExample = new PatrolTemplateSubsystemExample();
        PatrolTemplateSubsystemExample.Criteria ptsCriteria = ptsExample.createCriteria();
        ptsCriteria.andIsDeleteEqualTo(0);
        if (StrUtil.isNotBlank(subsystemTreeId)) {
            ptsCriteria.andSubsystemTreeIdEqualTo(Long.parseLong(subsystemTreeId));
        }
        if (StrUtil.isNotBlank(name)) {
            ptsCriteria.andNameLike("%" + name + "%");
        }
        boolean isPage = NumberUtil.isNumber(pageNum) && NumberUtil.isNumber(pageSize);
        Page<PatrolTemplateSubsystem> page = null;
        if (isPage) {
            page = PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        }
        List<PatrolTemplateSubsystem> ptsList = patrolTemplateSubsystemMapper.selectByExample(ptsExample);

        List<Map<String, Object>> list = new ArrayList<>();
        for (PatrolTemplateSubsystem pts : ptsList) {
            EquipmentTree et = etMap.get(pts.getSubsystemTreeId());
            Map<String, Object> map = new HashMap<>();
            map.put("id", pts.getId());
            map.put("subsystemTreeId", pts.getSubsystemTreeId());
            map.put("subsystemTreeName", et == null ? "" : et.getName());
            map.put("name", pts.getName());
            list.add(map);
        }
        if (isPage) {
            return DataResult.build9100(new GridData(page.getTotal(), list));
        } else {
            return DataResult.build9100(list);
        }
    }

    @Override
    public DataResult insertPatrolSubsystem(LoginInfo li, long subsystemTreeId, String name) throws Exception {
        PatrolTemplateSubsystem newPts = new PatrolTemplateSubsystem();
        newPts.setName(name);
        newPts.setSubsystemTreeId(subsystemTreeId);
        newPts.setIsDelete(0);
        patrolTemplateSubsystemMapper.insert(newPts);
        return DataResult.build9100(newPts.getId());
    }

    @Override
    public DataResult updatePatrolSubsystem(LoginInfo li, long id, long subsystemTreeId, String name) throws Exception {
        PatrolTemplateSubsystem newPts = new PatrolTemplateSubsystem();
        newPts.setId(id);
        newPts.setName(name);
        newPts.setSubsystemTreeId(subsystemTreeId);
        patrolTemplateSubsystemMapper.updateByPrimaryKeySelective(newPts);
        return DataResult.build9100(newPts.getId());
    }

    @Override
    public DataResult deletePatrolSubsystem(LoginInfo li, long id) throws Exception {
        PatrolTemplateSubsystem newPts = new PatrolTemplateSubsystem();
        newPts.setId(id);
        newPts.setIsDelete(1);
        patrolTemplateSubsystemMapper.updateByPrimaryKeySelective(newPts);
        return DataResult.build9100(id);
    }

    @Override
    public DataResult selectPatrolFixed(LoginInfo li, String pageNum, String pageSize, String name) throws Exception {
        PatrolTemplateFixedExample ptfExample = new PatrolTemplateFixedExample();
        PatrolTemplateFixedExample.Criteria ptfCriteria = ptfExample.createCriteria();
        ptfCriteria.andIsDeleteEqualTo(0);
        if (StrUtil.isNotBlank(name)) {
            ptfCriteria.andFixedNameLike("%" + name + "%");
        }
        boolean isPage = NumberUtil.isNumber(pageNum) && NumberUtil.isNumber(pageSize);
        Page<PatrolTemplateFixed> page = null;
        if (isPage) {
            page = PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        }
        List<PatrolTemplateFixed> ptfList = patrolTemplateFixedMapper.selectByExample(ptfExample);

        List<Map<String, Object>> list = new ArrayList<>();
        for (PatrolTemplateFixed ptf : ptfList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", ptf.getId());
            map.put("name", ptf.getFixedName());
            list.add(map);
        }
        if (isPage) {
            return DataResult.build9100(new GridData(page.getTotal(), list));
        } else {
            return DataResult.build9100(list);
        }
    }

    @Override
    public DataResult insertPatrolFixed(LoginInfo li, String name) throws Exception {
        PatrolTemplateFixed newPtf = new PatrolTemplateFixed();
        newPtf.setFixedName(name);
        newPtf.setIsDelete(0);
        patrolTemplateFixedMapper.insert(newPtf);
        return DataResult.build9100(newPtf.getId());
    }

    @Override
    public DataResult updatePatrolFixed(LoginInfo li, long id, String name) throws Exception {
        PatrolTemplateFixed newPtf = new PatrolTemplateFixed();
        newPtf.setId(id);
        newPtf.setFixedName(name);
        patrolTemplateFixedMapper.updateByPrimaryKeySelective(newPtf);
        return DataResult.build9100(newPtf.getId());
    }

    @Override
    public DataResult deletePatrolFixed(LoginInfo li, long id) throws Exception {
        PatrolTemplateFixed newPtf = new PatrolTemplateFixed();
        newPtf.setId(id);
        newPtf.setIsDelete(1);
        patrolTemplateFixedMapper.updateByPrimaryKeySelective(newPtf);
        return DataResult.build9100(newPtf.getId());
    }

    @Override
    public DataResult selectPatrolTask(LoginInfo li, int pageNum, int pageSize, int type, String typeId) throws Exception {
        PatrolTemplateTaskExample pttExample = new PatrolTemplateTaskExample();
        PatrolTemplateTaskExample.Criteria ttCriteria = pttExample.createCriteria();
        ttCriteria.andTypeEqualTo(type);
        if (NumberUtil.isNumber(typeId)) {
            int typeIdInt = Integer.parseInt(typeId);
            long typeIdLong = Long.parseLong(typeId);
            if (type == 1) {
                ttCriteria.andEmTypeEqualTo(typeIdInt);
            } else if (type == 2) {
                ttCriteria.andSubsystemIdEqualTo(typeIdLong);
            } else if (type == 3) {
                ttCriteria.andFixedIdEqualTo(typeIdLong);
            }
        }
        ttCriteria.andIsDeleteEqualTo(0);
        pttExample.setOrderByClause("sort asc");
        Page<PatrolTemplateTask> page = PageHelper.startPage(pageNum, pageSize);
        List<PatrolTemplateTask> pttList = patrolTemplateTaskMapper.selectByExample(pttExample);
        List<Map<String, Object>> list = new ArrayList<>();
        for (PatrolTemplateTask ptt : pttList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", ptt.getId());
            map.put("type", ptt.getType());
            if (ptt.getType() == 1) {
                map.put("typeId", ptt.getEmType());
            } else if (ptt.getType() == 2) {
                map.put("typeId", ptt.getSubsystemId());
            } else if (ptt.getType() == 3) {
                map.put("typeId", ptt.getFixedId());
            }
            map.put("project", ptt.getProject());
            map.put("way", ptt.getWay());
            map.put("standard", ptt.getStandard());
            map.put("isRecorded", ptt.getIsRecorded());
            map.put("sort", ptt.getSort());
            map.put("childType", ptt.getChildType());
            list.add(map);
        }
        return DataResult.build9100(new GridData(page.getTotal(), list));
    }

    @Override
    public DataResult insertPatrolTask(LoginInfo li, int type, int typeId, String project, String way, String standard, int isRecorded, String sort, String childType) throws Exception {
        PatrolTemplateTaskExample patrolTemplateTaskExample = new PatrolTemplateTaskExample();
        patrolTemplateTaskExample.createCriteria().andEmTypeEqualTo(typeId).andProjectEqualTo(project);
        List<PatrolTemplateTask> patrolTemplateTasks = patrolTemplateTaskMapper.selectByExample(patrolTemplateTaskExample);
        if (CollUtil.isNotEmpty(patrolTemplateTasks)) {
            return DataResult.build9200("点检项目名称重复：" + project);
        }
        PatrolTemplateTask newPtt = new PatrolTemplateTask();
        newPtt.setType(type);
        newPtt.setEmType(0);
        newPtt.setSubsystemId(0L);
        newPtt.setFixedId(0L);
        if (type == 1) {
            newPtt.setEmType(typeId);
        } else if (type == 2) {
            newPtt.setSubsystemId((long) typeId);
        } else if (type == 3) {
            newPtt.setFixedId((long) typeId);
        }
        newPtt.setProject(project);
        newPtt.setWay(way);
        newPtt.setStandard(standard);
        newPtt.setIsRecorded(isRecorded);
        newPtt.setSort(NumberUtil.isNumber(sort) ? Integer.parseInt(sort) : 0);
        newPtt.setIsDelete(0);
        if (ObjectUtil.isNotEmpty(childType)) {
            newPtt.setChildType(childType);
        }
        patrolTemplateTaskMapper.insert(newPtt);
        return DataResult.build9100(newPtt.getId());
    }

    @Override
    public DataResult updatePatrolTask(LoginInfo li, long id, int type, int typeId, String project, String way, String standard, int isRecorded, String sort, String childType) throws Exception {
        PatrolTemplateTaskExample patrolTemplateTaskExample = new PatrolTemplateTaskExample();
        patrolTemplateTaskExample.createCriteria().andEmTypeEqualTo(typeId).andProjectEqualTo(project).andIdNotEqualTo(id);
        List<PatrolTemplateTask> patrolTemplateTasks = patrolTemplateTaskMapper.selectByExample(patrolTemplateTaskExample);
        if (CollUtil.isNotEmpty(patrolTemplateTasks)) {
            return DataResult.build9200("点检项目名称重复：" + project);
        }
        PatrolTemplateTask newPtt = new PatrolTemplateTask();
        newPtt.setId(id);
        newPtt.setType(type);
        newPtt.setEmType(0);
        newPtt.setSubsystemId(0L);
        newPtt.setFixedId(0L);
        if (type == 1) {
            newPtt.setEmType(typeId);
        } else if (type == 2) {
            newPtt.setSubsystemId((long) typeId);
        } else if (type == 3) {
            newPtt.setFixedId((long) typeId);
        }
        newPtt.setProject(project);
        newPtt.setWay(way);
        newPtt.setStandard(standard);
        newPtt.setIsRecorded(isRecorded);
        newPtt.setSort(NumberUtil.isNumber(sort) ? Integer.parseInt(sort) : 0);
        if (ObjectUtil.isNotEmpty(childType)) {
            newPtt.setChildType(childType);
        }
        patrolTemplateTaskMapper.updateByPrimaryKeySelective(newPtt);
        return DataResult.build9100(newPtt.getId());
    }

    @Override
    public DataResult deletePatrolTask(LoginInfo li, long id) throws Exception {
        PatrolTemplateTask newPtt = new PatrolTemplateTask();
        newPtt.setId(id);
        newPtt.setIsDelete(1);
        patrolTemplateTaskMapper.updateByPrimaryKeySelective(newPtt);
        return DataResult.build9100(id);
    }

}
